package com.taskcollection.servlet;

import com.taskcollection.util.ConfigUtil;
import com.taskcollection.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/8/6 0006.
 */
@WebServlet(name = "WestosServlet")
public class WestosServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String[] path = servletPath.split("/");
        if(path.length!=3){
            response.getWriter().print("url 错误");
            return;
        }
        String meth = path[2].replace(".wst","");

        try {
            Object control = SpringUtil.getinstance().getFactory().getBean(path[1]);
            Class[] para = new Class[2];
            para[0] = HttpServletRequest.class;
            para[1] = HttpServletResponse.class;
            Method method = control.getClass().getMethod(meth,para);
            method.invoke(control,new Object[]{request,response});
        }  catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }  catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
